package org.liujing.jsw.client.bundle;

import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.*;
import com.google.gwt.core.client.GWT;

public class BundledImage{
	public boolean repeatX = false;
	public boolean repeatY = false;
	public String path;
	public int x;
	public int y;
	public int height;
	public int width;
	
	public BundledImage(String path, int x, int y, int width, int height, boolean repeatX, boolean repeatY){
		this.path = GWT.getModuleName() + "/" + path;
		this.x = x;
		this.y = y;
		this.repeatX = repeatX;
		this.repeatY = repeatY;
		this.width = width;
		this.height = height;
	}
	
	public void applyTo(Element el){
		setElementBackground(el, "url("+ path +")", -x, -y, repeatX, repeatY);
	}
	
	public void rotate(Element el){
		
	}
	
	private static native void setElementBackground(Element el, String path, int x, int y, boolean repeatX, boolean repeatY)/*-{
		var style = el.style;
		style.backgroundImage = path;
		style.backgroundPosition = x + "px " + y + "px";
		if(repeatX && repeatY)
			style.backgroundRepeat  = "repeat";
		else if(repeatX)
			style.backgroundRepeat  = "repeat-x";
		else if(repeatY)
			style.backgroundRepeat  = "repeat-y";
		else
			style.backgroundRepeat  = "no-repeat";
	}-*/;
	
	public String toString(){
		return "[BundledImage:" + path + ", x=" + x + ", y=" + y + ", w=" +
		width + ", h=" + height + (repeatX?" repeatX,":" ") + (repeatY?" repeatY]":"]");
	}
}
